package com.example.demo.day0301;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DemoBufferedInput {
    public static void main(String[] args) {
        try {//将抛出的异常捕获
            DemoBufferedInput.testBufferedInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void testBufferedInputStream() throws IOException {
        FileInputStream in = new FileInputStream("D:\\demo\\src\\main\\java\\com\\example\\demo\\day0301\\tt.txt");//文件字节流输入对象

        BufferedInputStream br = new BufferedInputStream(in);//把文件字节输入流换到缓冲字节输入流对象里

        byte[] b = new byte[10];//设置一个缓冲数组

        int len =0;

        while((len = br.read(b)) != -1){//直到br.read执行到没有字符串，此时返回值为-1。不再读取文件
            System.out.println(new String(b,0,len));//参数b是指转化的数组，参数0是指从次序为0的位置开始转化
        }
        br.close();//后用的流先关闭
        in.close();//先用的流后关闭
    }
}
